home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / histogram / init2d.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  6.6 KB  |  306 lines

  1. /* histogram/init2d.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <gsl/gsl_errno.h>
  24. #include <gsl/gsl_histogram2d.h>
  25.  
  26. gsl_histogram2d *
  27. gsl_histogram2d_alloc (const size_t nx, const size_t ny)
  28. {
  29.   gsl_histogram2d *h;
  30.  
  31.   if (nx == 0)
  32.     {
  33.       GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
  34.             GSL_EDOM, 0);
  35.     }
  36.  
  37.   if (ny == 0)
  38.     {
  39.       GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
  40.             GSL_EDOM, 0);
  41.     }
  42.  
  43.   h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
  44.  
  45.   if (h == 0)
  46.     {
  47.       GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
  48.             GSL_ENOMEM, 0);
  49.     }
  50.  
  51.   h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
  52.  
  53.   if (h->xrange == 0)
  54.     {
  55.       free (h);        /* exception in constructor, avoid memory leak */
  56.  
  57.       GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
  58.             GSL_ENOMEM, 0);
  59.     }
  60.  
  61.   h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
  62.  
  63.   if (h->yrange == 0)
  64.     {
  65.       free (h->xrange);
  66.       free (h);        /* exception in constructor, avoid memory leak */
  67.  
  68.       GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
  69.             GSL_ENOMEM, 0);
  70.     }
  71.  
  72.   h->bin = (double *) malloc (nx * ny * sizeof (double));
  73.  
  74.   if (h->bin == 0)
  75.     {
  76.       free (h->xrange);
  77.       free (h->yrange);
  78.       free (h);        /* exception in constructor, avoid memory leak */
  79.  
  80.       GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  81.             GSL_ENOMEM, 0);
  82.     }
  83.  
  84.   h->nx = nx;
  85.   h->ny = ny;
  86.  
  87.   return h;
  88. }
  89.  
  90. gsl_histogram2d *
  91. gsl_histogram2d_calloc_uniform (const size_t nx, const size_t ny,
  92.                 const double xmin, const double xmax,
  93.                 const double ymin, const double ymax)
  94. {
  95.   gsl_histogram2d *h;
  96.  
  97.   if (xmin >= xmax)
  98.     {
  99.       GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
  100.     }
  101.  
  102.   if (ymin >= ymax)
  103.     {
  104.       GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
  105.     }
  106.  
  107.   h = gsl_histogram2d_calloc (nx, ny);
  108.  
  109.   if (h == 0)
  110.     {
  111.       return h;
  112.     }
  113.  
  114.   {
  115.     size_t i;
  116.  
  117.     for (i = 0; i < nx + 1; i++)
  118.       {
  119.     h->xrange[i] = xmin + ((double) i / (double) nx) * (xmax - xmin);
  120.       }
  121.  
  122.     for (i = 0; i < ny + 1; i++)
  123.       {
  124.     h->yrange[i] = ymin + ((double) i / (double) ny) * (ymax - ymin);
  125.       }
  126.   }
  127.  
  128.   return h;
  129. }
  130.  
  131. gsl_histogram2d *
  132. gsl_histogram2d_calloc (const size_t nx, const size_t ny)
  133. {
  134.   gsl_histogram2d *h;
  135.  
  136.   if (nx == 0)
  137.     {
  138.       GSL_ERROR_VAL ("histogram2d length nx must be positive integer",
  139.             GSL_EDOM, 0);
  140.     }
  141.  
  142.   if (ny == 0)
  143.     {
  144.       GSL_ERROR_VAL ("histogram2d length ny must be positive integer",
  145.             GSL_EDOM, 0);
  146.     }
  147.  
  148.   h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d));
  149.  
  150.   if (h == 0)
  151.     {
  152.       GSL_ERROR_VAL ("failed to allocate space for histogram2d struct",
  153.             GSL_ENOMEM, 0);
  154.     }
  155.  
  156.   h->xrange = (double *) malloc ((nx + 1) * sizeof (double));
  157.  
  158.   if (h->xrange == 0)
  159.     {
  160.       free (h);        /* exception in constructor, avoid memory leak */
  161.  
  162.       GSL_ERROR_VAL ("failed to allocate space for histogram2d x ranges",
  163.             GSL_ENOMEM, 0);
  164.     }
  165.  
  166.   h->yrange = (double *) malloc ((ny + 1) * sizeof (double));
  167.  
  168.   if (h->yrange == 0)
  169.     {
  170.       free (h->xrange);
  171.       free (h);        /* exception in constructor, avoid memory leak */
  172.  
  173.       GSL_ERROR_VAL ("failed to allocate space for histogram2d y ranges",
  174.             GSL_ENOMEM, 0);
  175.     }
  176.  
  177.   h->bin = (double *) malloc (nx * ny * sizeof (double));
  178.  
  179.   if (h->bin == 0)
  180.     {
  181.       free (h->xrange);
  182.       free (h->yrange);
  183.       free (h);        /* exception in constructor, avoid memory leak */
  184.  
  185.       GSL_ERROR_VAL ("failed to allocate space for histogram bins",
  186.             GSL_ENOMEM, 0);
  187.     }
  188.  
  189.   {
  190.     size_t i;
  191.  
  192.     for (i = 0; i < nx + 1; i++)
  193.       {
  194.     h->xrange[i] = i;
  195.       }
  196.  
  197.     for (i = 0; i < ny + 1; i++)
  198.       {
  199.     h->yrange[i] = i;
  200.       }
  201.  
  202.     for (i = 0; i < nx * ny; i++)
  203.       {
  204.     h->bin[i] = 0;
  205.       }
  206.   }
  207.  
  208.   h->nx = nx;
  209.   h->ny = ny;
  210.  
  211.   return h;
  212. }
  213.  
  214.  
  215. void
  216. gsl_histogram2d_free (gsl_histogram2d * h)
  217. {
  218.   free (h->xrange);
  219.   free (h->yrange);
  220.   free (h->bin);
  221.   free (h);
  222. }
  223.  
  224.  
  225. int 
  226. gsl_histogram2d_set_ranges_uniform (gsl_histogram2d * h, 
  227.                                     double xmin, double xmax,
  228.                                     double ymin, double ymax)
  229. {
  230.   size_t i;
  231.   const size_t nx = h->nx, ny = h->ny;
  232.  
  233.   if (xmin >= xmax)
  234.     {
  235.       GSL_ERROR_VAL ("xmin must be less than xmax", GSL_EINVAL, 0);
  236.     }
  237.  
  238.   if (ymin >= ymax)
  239.     {
  240.       GSL_ERROR_VAL ("ymin must be less than ymax", GSL_EINVAL, 0);
  241.     }
  242.  
  243.   /* initialize ranges */
  244.  
  245.   for (i = 0; i <= nx; i++)
  246.     {
  247.       h->xrange[i] = xmin + ((double) i / (double) nx) * (xmax - xmin);
  248.     }
  249.  
  250.   for (i = 0; i <= ny; i++)
  251.     {
  252.       h->yrange[i] = ymin + ((double) i / (double) ny) * (ymax - ymin);
  253.     }
  254.  
  255.   /* clear contents */
  256.  
  257.   for (i = 0; i < nx * ny; i++)
  258.     {
  259.       h->bin[i] = 0;
  260.     }
  261.  
  262.   return GSL_SUCCESS;
  263. }
  264.  
  265. int 
  266. gsl_histogram2d_set_ranges (gsl_histogram2d * h, 
  267.                             const double xrange[], size_t xsize,
  268.                             const double yrange[], size_t ysize)
  269. {
  270.   size_t i;
  271.   const size_t nx = h->nx, ny = h->ny;
  272.  
  273.   if (xsize != (nx + 1))
  274.     {
  275.       GSL_ERROR_VAL ("size of xrange must match size of histogram", 
  276.                      GSL_EINVAL, 0);
  277.     }
  278.  
  279.   if (ysize != (ny + 1))
  280.     {
  281.       GSL_ERROR_VAL ("size of yrange must match size of histogram", 
  282.                      GSL_EINVAL, 0);
  283.     }
  284.  
  285.   /* initialize ranges */
  286.  
  287.   for (i = 0; i <= nx; i++)
  288.     {
  289.       h->xrange[i] = xrange[i];
  290.     }
  291.  
  292.   for (i = 0; i <= ny; i++)
  293.     {
  294.       h->yrange[i] = yrange[i];
  295.     }
  296.  
  297.   /* clear contents */
  298.  
  299.   for (i = 0; i < nx * ny; i++)
  300.     {
  301.       h->bin[i] = 0;
  302.     }
  303.  
  304.   return GSL_SUCCESS;
  305. }
  306.